<template>
  <div class="divBox">
    <div class="title_name">实 验 室 安 全 准 入 教 育</div>
    <div class="container right-panel-active">
      <!-- 注册 -->
      <div class="container__form container--signup">
        <form class="form">
          <h2 class="form__title">注册账号</h2>
          <input
            type="text"
            placeholder="请输入账号"
            class="input"
            v-model="Rusernumber"
          />
          <input
            type="password"
            placeholder="请输入密码"
            class="input"
            v-model="Rpassword"
          />
          <input
            type="text"
            placeholder="请输入姓名"
            class="input"
            v-model="Rusername"
          />
          <input
            type="text"
            placeholder="请输入班级"
            class="input"
            v-model="Rclass"
          />
          <button type="button" class="btn" @click="registerIn" id="sign">
            点击注册
          </button>
        </form>
      </div>
      <!-- 登录 -->
      <div class="container__form container--signin">
        <form class="form">
          <h2 class="form__title">欢迎登录</h2>
          <input
            type="text"
            placeholder="请输入账号"
            class="input"
            v-model="Lusername"
          />
          <input
            type="password"
            placeholder="请输入密码"
            class="input"
            v-model="Lpassword"
          />
          <!-- <el-link style="margin-top: 5%" @click="forgiveIn()" type="primary" icon="el-icon-edit">忘 记 密 码</el-link> -->
          <button type="button" class="btn" @click="goIn">登录</button>
        </form>
      </div>

      <!-- 叠加部分 -->
      <div class="container__overlay">
        <div class="overlay">
          <div class="overlay__panel overlay--left">
            <button class="btn" id="signIn">已有账号，直接登录</button>
          </div>
          <div class="overlay__panel overlay--right">
            <button class="btn" id="signUp">没有账号，点击注册</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  created() {
    this.WeiXiu();
  },
  data() {
    return {
      Lusername: "",
      Lpassword: "",
      Rusernumber: "",
      Rpassword: "",
      Rusername: "",
      Rclass: "",
      // 维修弹窗
      OutlogVisible: false,
      WeiXiuTittle: "",
    };
  },
  methods: {
    // 维修弹窗
    async WeiXiu() {
      const { data: res } = await this.$http.post("/api/findgonggao ");
      // console.log(res);
      if (res.code == 200) {
        this.WeiXiuTittle = res.data[0].gonggao;
        this.$notify({
          title: "公告",
          message: this.WeiXiuTittle,
          type: "warning",
          duration: 0,
        });
        return;
      } else {
        this.$message.error("维修提示框显示失败");
        return;
      }
    },

    // 实现左右滑动
    show() {
      // 获取元素和点击操作
      const signInBtn = document.getElementById("signIn");
      const signUpBtn = document.getElementById("signUp");
      const container = document.querySelector(".container");

      signInBtn.addEventListener("click", () => {
        container.classList.remove("right-panel-active");
      });

      signUpBtn.addEventListener("click", () => {
        container.classList.add("right-panel-active");
      });
    },
    // 登录
    async goIn() {
      // console.log(this.Lusername)
      // console.log(this.Lpassword)
      if (this.Lusername === "") {
        this.$message({
          message: "请输入用户名！",
          type: "warning",
        });
        return false;
      } else if (this.Lpassword === "") {
        this.$message({
          message: "请输入密码！",
          type: "warning",
        });
        return false;
      }
      const { data: res } = await this.$http.post("/api/users/login", {
        account: this.Lusername,
        password: this.Lpassword,
      });
      // console.log(res);
      if (res.code != 200) {
        this.$message.error(res.msg);
        return;
      }
      this.$message.success("登录成功！");
      window.sessionStorage.setItem("token", "Bearer " + res.data);
      this.$router.push("/Home");
    },
    // 注册
    async registerIn() {
      // console.log(this.Rusername)
      // console.log(this.Rpassword)
      if (this.Rusernumber === "") {
        this.$message({
          message: "请输入账号！",
          type: "warning",
        });
        return false;
      } else if (this.Rpassword === "") {
        this.$message({
          message: "请输入密码！",
          type: "warning",
        });
        return false;
      } else if (this.Rusername === "") {
        this.$message({
          message: "请输入姓名！",
          type: "warning",
        });
        return false;
      } else if (this.Rclass === "") {
        this.$message({
          message: "请输入班级！",
          type: "warning",
        });
        return false;
      }
      const { data: res } = await this.$http.post("/api/users/registered", {
        account: this.Lusername,
        password: this.Lpassword,
        name: this.Rusername,
        class: this.Rclass,
      });
      // console.log(res);
      if (res.code != 200) {
        this.$message.error(res.msg);
        return;
      }
      this.$message.success(res.msg);
    },
  },
  mounted() {
    this.show();
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.title_name {
  color: #fff;
  font-size: 60px;
  text-align: center;
}
.divBox {
  /*  字体样式  */
  font-size: 16px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
  /* 弹性盒子 垂直居中 */
  align-items: center;
  /* 背景颜色为自定义属性 --white */
  background-color: rgba(0, 0, 0, 0.2);
  /* 背景图片 */
  background: url("../assets/images/space.jpg");
  /* 背景固定。即使一个元素拥有滚动机制，背景也不会随着元素的内容滚动。 */
  background-attachment: fixed;
  /* 图片居中 */
  background-position: center;
  /* background-repeat定义背景图像的重复方式，no-repeat背景图像将仅显示一次 */
  background-repeat: no-repeat;
  /* 把背景图片放大到适合元素容器的尺寸*/
  background-size: cover;
  /* 网格布局*/
  display: grid;
  /* 设置height:100vh，能够保证元素无论是否有没有内容，高度都等于屏幕高度。*/
  height: 100vh;
  /* 沿着对齐容器的中心对齐。*/
  place-items: center;
}

/* 标题 */
.form__title {
  /* 字体粗细 */
  font-weight: 300;
  margin: 0;
  margin-bottom: 2.25rem;
}

/* 忘记密码字体样式 */
.link {
  color: #333;
  font-size: 0.9rem;
  margin: 1.5rem 0;
  text-decoration: none;
}

/* 主体DIV样式 */
.container {
  position: absolute;
  top: 0%;
  left: 0%;
  transform: translate(0%, -10%);
  background-color: #e9e9e9;
  border-radius: 0.7rem;
  /* 添加阴影效果 */
  box-shadow: 0 0.9rem 1.7rem rgba(0, 0, 0, 0.25),
    0 0.7rem 0.7rem rgba(0, 0, 0, 0.22);
  height: 500px;
  max-width: 758px;
  overflow: hidden;
  position: relative;
  width: 100%;
}

/* 登录、注册框部分 */
.container__form {
  height: 100%;
  position: absolute;
  top: 0;
  transition: all 0.6s ease-in-out;
}

.container--signin {
  left: 0;
  width: 50%;
  z-index: 2;
}

.container.right-panel-active .container--signin {
  transform: translateX(100%);
}

.container--signup {
  left: 0;
  opacity: 0;
  width: 50%;
  z-index: 1;
}

.container.right-panel-active .container--signup {
  animation: show 0.6s;
  opacity: 1;
  transform: translateX(100%);
  z-index: 5;
}

/* 叠加部分样式 */
.container__overlay {
  height: 100%;
  left: 50%;
  overflow: hidden;
  position: absolute;
  top: 0;
  transition: transform 0.6s ease-in-out;
  width: 50%;
  z-index: 100;
}

.container.right-panel-active .container__overlay {
  transform: translateX(-100%);
}

.overlay {
  background-color: #008997;
  background: url("../assets/images/space.jpg");
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 100%;
  left: -100%;
  position: relative;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
  width: 200%;
}

.container.right-panel-active .overlay {
  transform: translateX(50%);
}

.overlay__panel {
  align-items: center;
  display: flex;
  flex-direction: column;
  height: 100%;
  justify-content: center;
  position: absolute;
  text-align: center;
  top: 0;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
  width: 50%;
}

.overlay--left {
  transform: translateX(-20%);
}

.container.right-panel-active .overlay--left {
  transform: translateX(0);
}

.overlay--right {
  right: 0;
  transform: translateX(0);
}

.container.right-panel-active .overlay--right {
  transform: translateX(20%);
}

/* 按钮设置 */
.btn {
  background-color: #0367a6;
  background-image: linear-gradient(90deg, #0367a6 0%, #008997 74%);
  border-radius: 20px;
  border: 1px solid #0367a6;
  color: #e9e9e9;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: bold;
  letter-spacing: 0.1rem;
  padding: 0.9rem 4rem;
  text-transform: uppercase;
  transition: transform 80ms ease-in;
}

.form > .btn {
  margin-top: 1.5rem;
}

.btn:active {
  transform: scale(0.95);
}

.btn:focus {
  outline: none;
}

/* 表单样式 */
.form {
  background-color: #e9e9e9;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 0 3rem;
  height: 100%;
  text-align: center;
}

.input {
  background-color: #fff;
  border: none;
  padding: 0.9rem 0.9rem;
  margin: 0.5rem 0;
  width: 100%;
}

@keyframes show {
  0%,
  49.99% {
    opacity: 0;
    z-index: 1;
  }

  50%,
  100% {
    opacity: 1;
    z-index: 5;
  }
}
</style>
